﻿//-----------------------------------------------------------------------
// <copyright file="WorldManager.cs" company="Minecraft Tools Project">
//      Copyright Laurie Tutton. All Rights Reserved
// </copyright>
//-----------------------------------------------------------------------

namespace Minecraft.Imaging
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// This class is the main entry point for the imaging library. It handles the main bulk of rendering things.
    /// </summary>
    public class WorldRenderer : IMapRenderer
    {
        /// <summary>
        /// Renders the chunks in the map
        /// </summary>
        /// <param name="settings">The settings to render with</param>
        /// <returns>the rendered chunks in an image</returns>
        public System.Drawing.Image RenderMap(IMapSettings settings)
        {
            // Make the largest rectangle we can
            Rectangle bigRect = new Rectangle(int.MinValue, int.MinValue, int.MaxValue, int.MaxValue);
            return RenderMapArea(bigRect, settings);
        }

        /// <summary>
        /// Renders the chunks within the area in chunk coordinates
        /// </summary>
        /// <param name="updateArea">The rectangle to update</param>
        /// <param name="settings">The settings to render with</param>
        /// <returns>The rendered chunks in an image</returns>
        public System.Drawing.Image RenderMapArea(Rectangle updateArea, IMapSettings settings)
        {
            // Get the regions for the folder in the settings
#if DEBUG
            DateTime startTime = DateTime.Now;
#endif

            string searchPath = settings.WorldPath;
            if (settings.IsHell)
            {
                searchPath = Path.Combine(searchPath, "DIM-1");
            }
            searchPath = Path.Combine(searchPath, "region");
            List<string> regionPaths = GetFolder(searchPath);
            int chunkCount = 0;
            foreach (string regionPath in regionPaths)
            {
                Region currentRegion = new Region(regionPath);
                chunkCount += currentRegion.ChunkCount;
            }
#if DEBUG
            TimeSpan elapsed = DateTime.Now - startTime;
            Console.Out.WriteLine("Found {0} chunks in {1} regions in {2}ms", chunkCount, regionPaths.Count, elapsed.TotalMilliseconds);
#endif

            throw new NotImplementedException();
        }

        /// <summary>
        /// Recursively looks up all .mcr files inside a folder
        /// </summary>
        /// <param name="folder">The starting folder</param>
        /// <returns>A list of region files</returns>
        private List<string> GetFolder(string folder)
        {
            List<string> files = new List<string>();
            foreach (string directory in Directory.GetDirectories(folder))
            {
                if (!directory.Substring(directory.LastIndexOf('\\') + 1).StartsWith("DIM-"))
                {
                    files.AddRange(this.GetFolder(directory));
                }
            }

            foreach (string fileItem in Directory.GetFiles(folder, "*.mcr"))
            {
                files.Add(fileItem);
            }

            return files;
        }
    }
}